<?php
/* 
 * EachGo电商网站 新媒体
 * www.eachgo.cn
 */
namespace Home\Controller;
use Think\Controller;
class ShareController extends Controller{
    public function saveDate(){//保存数据
        if (!empty($_POST)) {
            //判断附件是否有上传
            //如果有则实例化Upload，把附件上传到服务器指定位置
            //然后获得附件的路径名，存入$_POST
            //var_dump($_FILES);
            if (!empty($_FILES)) {
                //var_dump($_FILES);
                $config = array(
                    'rootPath' => './Application/Home/upload/',//保存根目录
                    'savePath' => 'Share/',      //保存路径
                );
                $upload = new \Think\Upload($config);
                $z = $upload->uploadOne($_FILES['image']);
                if (!$z) {
                    var_dump($upload->getError());
                } else {
                    //拼装图片的路径名
                    $bigimg = $z['savepath'] . $z['savename'];
                    $_POST['imageURL'] = $bigimg;
                }
                $userInfo=session('UserInfo');
                //var_dump($_POST);
                $data=array(//其他保存是需要的数据
                    'user_ID'=>$userInfo['id'],
                    'shareTime'=>date('Y-m-d h:i:s'),
                );
                $data=array_merge($_POST,$data);
               return $data;
            }
        }
    }
    public function saveShare(){//保存为草稿
        $share=D("Share");
        $shareController=A("Share");
        $data=$shareController->saveDate($_POST);
        $isRelease=array(
            'isShared'=>false,
        );
        $data=array_merge($data,$isRelease);
        //var_dump($data);
        $exist=$share->findById($data['userLotteryInfo_ID']);
        if(!empty($exist)){
            $shareId=$share->update($data);//判断该条数据是否存在，如果存在则更新，不存在则增加
        }else{
            $shareId=$share->addShare($data);
        }
        //var_dump($data);
        $shareController->showShare();
       // $this->display("User/myShare");
    }
    public function addShare()//增加一条新的分享
    {
        $share=D("Share");
        $shareController=A("Share");
        $data=$shareController->saveDate($_POST);//保存数据
        $isRelease=array(
            'isShared'=>true,
        );
        $data=array_merge($data,$isRelease);
       // var_dump($data);
        $shareId=$share->addShare($data);
       // $this->display("User/myShare");
        $shareController->showShare();
    }
    public function editShare(){//编辑新的分享
        //var_dump($_GET);
        $share=D('Share');
        $userLottery=D("UserLotteryInfo");
        $goodsInfo=$userLottery->shareInfo($_GET['userlotterId']);
//        if(!empty($_GET['shareId'])){
//            $oldInfo=$share->findById($_GET['shareId']);
//        }
        $oldInfo=$share->findById($_GET['userlotterId']);
        //获取已经存在分享的草稿的内容
        //var_dump($oldInfo);
        $this->assign('oldInfo',$oldInfo[0]);
        //var_dump($goodsInfo[0]);
        $this->assign('goodsInfo',$goodsInfo[0]);
        $this->display("User/editShare");
    }
    public function delShare(){//删除一条分享
        var_dump($_GET);
        $share=D('Share');
        $share->deleteShare($_GET['shareId']);
        $shareController=A('Share');
        $shareController->showShare();
    }
    public function showShare(){//显示所有分享
        $userInfo=session('UserInfo');
        $share=D("Share");
        $shareInfo=$share->queryAll($userInfo['id']);
        //var_dump($shareInfo);
        $this->assign('shareInfo',$shareInfo);
        $this->display("User/myShare");
    }
    public function getShareByUserId(){
        $share=D('Share');
        $result=$share->queryByUserId(2);
        var_dump($result);
    }
    public function getShareByType(){
        $share=D('Share');
        $result=$share->queryByType(1);
        var_dump($result);
    }
    public function updateShare(){
        $share=D('Share');
        $data=array(
            'id'=>2,
            'shareTime'=>date('Y-m-d h:i:s'),
            'shareText'=>'you are sun of bigncega',
            'title'=>'shit',
        );
        $result=$share->update($data);
        var_dump($result);
    }
    public function showOneShare(){
        var_dump($_GET);

    }
}
